package com.hoperun.utils;

import java.beans.PropertyEditorSupport;

import org.apache.commons.lang.StringEscapeUtils;

/**
 * Copyright: Copyright (c) 2014 CNPCMALL <br/>
 * ClassName: StringEscapeEditor.java
 * 
 * @Description: 该类的功能描述
 * 
 * @version: v1.0.0
 * @author: fuyu
 * @date: 2014-8-14 下午1:37:22
 * 
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2014-8-14
 *        fuyu v1.0.0 修改原因
 */
public class StringEscapeEditor extends PropertyEditorSupport
{
	
	private boolean escapeHTML;
	
	private boolean escapeJavaScript;
	
	private boolean escapeSQL;
	
	public StringEscapeEditor()
	{
		super();
	}
	
	public StringEscapeEditor(boolean escapeHTML, boolean escapeJavaScript,
			boolean escapeSQL)
	{
		super();
		this.escapeHTML = escapeHTML;
		this.escapeJavaScript = escapeJavaScript;
		this.escapeSQL = escapeSQL;
		
	}
	
	@Override
	public String getAsText()
	{
		Object value = getValue();
		return value != null ? value.toString() : "";
	}
	
	@Override
	public void setAsText(String text)
		throws IllegalArgumentException
	{
		if (text == null)
		{
			setValue(null);
		}
		else
		{
			String value = text;
			if (escapeHTML)
			{
				value = StringEscapeUtils.escapeHtml(value);
			}
			if (escapeJavaScript)
			{
				value = StringEscapeUtils.escapeJavaScript(value);
			}
			if (escapeSQL)
			{
				value = StringEscapeUtils.escapeSql(value);
			}
			super.setValue(value);
		}
	}
}
